﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using EyesOfTheDragon.Events;
using XRpgLibrary.Events;
using Anthem.GameScreens;
using Anthem.Components;
using EyesOfTheDragon.Events;

namespace EyesOfTheDragon.Components
{
    public class HealthMeter :IUpdate, IPauseable
    {
        private bool _enabled = true;
        private Texture2D _base;
        private Texture2D _bar;
        private Texture2D _overlay;
        private Texture2D _boss_Bar;
        private Rectangle DrawPosition = new Rectangle(650, 5, 100, 36);
        private Rectangle BossDrawPosition = new Rectangle(250, 5, 100, 36);
        private SpriteFont _spriteFont;
        public float _roundTime = 120;
       // Vector2 Position = new Vector2(500, 50);
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        protected string roundTimeToString()
        {
            int seconds = (int)_roundTime % 60;
            string __seconds = seconds.ToString();
            if (seconds < 10)
                __seconds = "0" + __seconds;
            return "" + (int)_roundTime / 60 + ":" + __seconds;
        }
        public HealthMeter()
        {
            _base = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"GUI\HealthBase");
            _bar = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"GUI\HealthMeter");
            _boss_Bar = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"GUI\BossMeter");
            _overlay = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"GUI\HealthOverlay");
            _spriteFont = GamePlayScreen.Instance.GameRef.Content.Load<SpriteFont>(@"Fonts\ControlFont");
        }
        public static HealthMeter Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new HealthMeter();
                return _instance;
            }
        }
        public void Update(GameTime gt)
        {
            _roundTime -= gt.ElapsedGameTime.Milliseconds / 1000f;
        }
        public void Draw(GameTime gt, SpriteBatch sp)
        {

            if (Enabled)
            {
                float percent = Player.Instance.Health/100f;
                sp.Draw(_base, DrawPosition, Color.White);
                sp.Draw(_bar, new Rectangle(DrawPosition.X, DrawPosition.Y, (int)(DrawPosition.Width * percent), DrawPosition.Height), Color.White) ;
                sp.Draw(_overlay, DrawPosition, Color.White);
                sp.DrawString(_spriteFont, "Countdown To Massacre:", new Vector2(DrawPosition.X-DrawPosition.Width, DrawPosition.Y  +DrawPosition.Height), Color.Red);
                sp.DrawString(_spriteFont, roundTimeToString(), new Vector2(DrawPosition.X, _spriteFont.MeasureString("999").Y + 10 + DrawPosition.Height), Color.Red);

                if (FinalBoss.Instance != null && FinalBoss.Instance.Enabled)
                {
                    percent = FinalBoss.Instance.Health / 100f;
                    sp.Draw(_base, BossDrawPosition, Color.White);
                    sp.Draw(_boss_Bar, new Rectangle(BossDrawPosition.X, BossDrawPosition.Y, (int)(BossDrawPosition.Width * percent), BossDrawPosition.Height), Color.White);
                    sp.Draw(_overlay, BossDrawPosition, Color.White);
                    sp.DrawString(_spriteFont, "Final Boss", new Vector2(BossDrawPosition.X - BossDrawPosition.Width, BossDrawPosition.Y + BossDrawPosition.Height), Color.Blue);
                }
                else
                {
                    if (map.Instance.CurrentMap > 1)
                    sp.DrawString(_spriteFont, "Monsters Remaining: " + MonsterGenerator.Instance.MonstersRemaining, new Vector2(BossDrawPosition.X - BossDrawPosition.Width, BossDrawPosition.Y + BossDrawPosition.Height), Color.Blue);
                }
            }
        }


        private static HealthMeter _instance;
    }
}
